package mark.tom.detector;

import android.support.annotation.NonNull;
import android.util.Base64;

import java.io.Closeable;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.zip.ZipFile;

import mark.tom.detector.template.Logger;

import static mark.tom.detector.C.TIMBER_KEY_CLASS_PATH;

final class Util {

    static void checkNull(String message, Object value) {
        if (value == null) {
            throw new NullPointerException(message);
        }
    }

    static void checkNullOrEmpty(String message, String value) {
        if (isEmpty(value)) {
            throw new NullPointerException(message + " should not be null or empty");
        }
    }

    private static boolean isEmpty(String text) {
        return text == null || text.trim().length() == 0;
    }

    private static boolean isClassPresent(String className) {
        boolean result = false;
        try {
            result = (null != Class.forName(className));
        } catch (Exception ex) {
            // ignore
        }
        return result;
    }

    /**
     * @Description: 获取日志记录器
     * @Author: ttmk110@163.com
     * @Date: 2020/3/19
     */
    static Logger getLogger(boolean onoff) {
        Logger result = null;
        // 判断是否依赖了Timer
        if (isClassPresent(TIMBER_KEY_CLASS_PATH)) {
            result = new TimberLogger();
        }
        if (null == result) {
            result = new AndroidLogger();
        }
        result.onOff(onoff);
        return result;
    }

    static String getSHA256DigestByFileName(@NonNull String source, @NonNull String fileName) throws Exception {
        String result;
        InputStream is = null;
        try {
            is = getInputStreamByFileInfo(source, fileName);
            result = getDigest("SHA-256", is);
        } finally {
            closeQuietly(is);
        }
        return result;
    }

    static String getSHA1DigestByFileName(@NonNull String source, @NonNull String fileName) throws Exception {
        String result;
        InputStream is = null;
        try {
            is = getInputStreamByFileInfo(source, fileName);
            result = getDigest("SHA-1", is);
        } finally {
            closeQuietly(is);
        }
        return result;
    }

    private static InputStream getInputStreamByFileInfo(@NonNull String source, @NonNull String fileName) throws Exception {
        ZipFile zFile = new ZipFile(source);
        return zFile.getInputStream(zFile.getEntry(fileName));
    }

    private static String getDigest(@NonNull String algorithm, @NonNull InputStream is) throws Exception {
        MessageDigest dexDigest = MessageDigest.getInstance(algorithm);
        byte[] bytes = new byte[1024];
        int byteCount;
        while ((byteCount = is.read(bytes)) != -1) {
            dexDigest.update(bytes, 0, byteCount);
        }
        String result = Base64.encodeToString(dexDigest.digest(), Base64.DEFAULT);
        int wrapIndex = result.indexOf("\n");
        if (wrapIndex != -1) {
            result = result.subSequence(0, wrapIndex).toString();
        }
        return result;
    }

    private static void closeQuietly(Closeable c) {
        if (null == c) {
            return;
        }
        try {
            c.close();
        } catch (Exception ex) {
            // ignore
        }
    }
}
